from pydantic import BaseModel, Field
from typing import Optional
from datetime import datetime, date
from .response import SuccessResponse


class UserLoginRequest(BaseModel):
    """微信登录请求"""
    code: str = Field(..., description="微信授权码")


class UserInfo(BaseModel):
    """用户信息"""
    id: int = Field(..., description="用户ID")
    openid: str = Field(..., description="微信openid")
    nickname: Optional[str] = Field(None, description="昵称")
    avatar: Optional[str] = Field(None, description="头像")
    phone: Optional[str] = Field(None, description="手机号")
    points: int = Field(0, description="积分余额")
    
    # 打卡相关字段
    is_checkin: bool = Field(False, description="今日是否已打卡", alias="isCheckin")
    total_checkin_days: int = Field(0, description="总打卡天数", alias="totalCheckinDays")
    continuous_checkin_days: int = Field(0, description="当前连续打卡天数", alias="continuousCheckinDays")
    last_checkin_date: Optional[date] = Field(None, description="最后打卡日期", alias="lastCheckinDate")
    
    created_at: datetime = Field(..., description="创建时间", alias="createdAt")
    updated_at: datetime = Field(..., description="更新时间", alias="updatedAt")
    
    class Config:
        populate_by_name = True
        from_attributes = True


class LoginResponse(BaseModel):
    """登录响应数据"""
    access_token: str = Field(..., description="访问令牌")
    refresh_token: str = Field(..., description="刷新令牌")
    token_type: str = Field(default="Bearer", description="令牌类型")
    expires_in: int = Field(..., description="访问令牌过期时间(秒)")
    user_info: UserInfo = Field(..., description="用户信息", alias="userInfo")
    
    class Config:
        populate_by_name = True


class UpdateUserRequest(BaseModel):
    """更新用户信息请求"""
    nickname: Optional[str] = Field(None, description="昵称")
    avatar: Optional[str] = Field(None, description="头像URL")
    
    class Config:
        populate_by_name = True





class RefreshTokenRequest(BaseModel):
    """刷新令牌请求"""
    refresh_token: str = Field(..., description="刷新令牌")


class RefreshTokenResponse(BaseModel):
    """刷新令牌响应数据"""
    access_token: str = Field(..., description="新的访问令牌")
    token_type: str = Field(default="Bearer", description="令牌类型")
    expires_in: int = Field(..., description="访问令牌过期时间(秒)")
    
    class Config:
        populate_by_name = True


# 响应类型定义
UserLoginResponse = SuccessResponse[LoginResponse]
UserProfileResponse = SuccessResponse[UserInfo]
UserUpdateResponse = SuccessResponse[UserInfo]
RefreshTokenApiResponse = SuccessResponse[RefreshTokenResponse]